package com.intellagric.agriecom.service.agriecom_produce;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.agriecom.module.agriecom_produce.ProduceResult;
import com.intellagric.agriecom.module.agriecom_produce.ProduceService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_produce.AgriecomProduceDao;
import com.intellagric.mapper.AgriecomProduceCategoryMapper;
import com.intellagric.mapper.AgriecomProduceMapper;
import com.intellagric.mapper.AgriecomProduceParamMapper;
import com.intellagric.mapper.AgriecomProduceParamValueMapper;
import com.intellagric.module.agriecom.agriecom_produce.ParamValueVo;
import com.intellagric.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




import java.util.*;


/**
 * @Autuor cky
 * @Date 2018/11/10 15:23
*/
@Service
public class ProduceServiceImpl implements ProduceService {

    @Autowired
    AgriecomProduceMapper dao;

    @Autowired
    AgriecomProduceCategoryMapper cateDao;

    @Autowired
    AgriecomProduceDao agriecomProduceDao;

    @Autowired
    AgriecomProduceParamMapper paramMapper;

    @Autowired
    AgriecomProduceParamValueMapper paramValueMapper;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public ResponseMessage selectProduceById(String id) {
        if(id!=null&&!id.equals("")){
            ResponseMessage msg=new ResponseMessage();
            msg.add("produce",dao.selectByPrimaryKey(id));
            msg.setTotal(1L);
            msg.setMessage("查询成功");
            return msg;
        }
        return ResponseMessage.fail("传入的id为空");
    }

    /**
     *  Ade
     *  返回的是AgriecomProduce对象
     * @param id
     * @return
     */
    @Override
    public AgriecomProduce queryProduceById(String id) {
        if(id!=null&&id!="")
            return dao.selectByPrimaryKey(id);
        return null;
    }

    /**
     * 插入一条数据
     * @param produce
     * @return
     */
    @Override
    public ResponseMessage insertProduce(AgriecomProduce produce) {
        int r=dao.insertSelective(produce);
        if(r==0){
            return ResponseMessage.success("插入失败");
        }
        return ResponseMessage.success();
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteProduce(String id) {
        int r=dao.deleteByPrimaryKey(id);
        if(r==0)
            return ResponseMessage.fail("删除失败");
        return ResponseMessage.success();
    }

    /**
     *  Ade
     * 批量删除数据
     * @param ids
     * @return
     */

    @Override
    public ResponseMessage batchDeleteProduce(String[] ids) {

        if (agriecomProduceDao.batchDeleteByPrimaryKey(ids)<1)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }



    /**
     * 编辑数据
     * @param produce
     * @return
     */
    @Override
    public ResponseMessage editProduce(AgriecomProduce produce) {
        if(produce!=null){
            AgriecomProduceExample example =new AgriecomProduceExample();
            example.createCriteria().andProduceIdEqualTo(produce.getProduceId());
            produce.setUpdateDate(new Date(System.currentTimeMillis()));
            int r=dao.updateByExampleSelective(produce,example);
            if(r!=0)
                return ResponseMessage.success();
        }
        return ResponseMessage.fail();
    }

    /**
     * 获得分页
     * @param page
     * @param rows
     * @return
     */
    @Override
    public LayuiDataGridResult getList(int page, int rows,String order) {
        PageHelper.startPage(page,rows);
        AgriecomProduceExample example=new AgriecomProduceExample();
        example.createCriteria().andProduceIdIsNotNull();
        //按价格排序
        if(order!=null&&!order.equals(""))
            example.setOrderByClause(order);

        List<AgriecomProduce> proList=dao.selectByExampleWithBLOBs(example);
        PageInfo<AgriecomProduce> pageInfo = new PageInfo<>(proList);
        List<ProduceResult> resultList=new ArrayList<>();
        for (AgriecomProduce pro: proList){
            AgriecomProduceCategory category=cateDao.selectByPrimaryKey(pro.getCategoryId());
            ProduceResult result=new ProduceResult(pro);
            if(category!=null)
                result.setCategoryName(category.getName());
            resultList.add(result);
        }
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(resultList,pageInfo.getTotal());
        lay.setCode(0);
        lay.setMsg("分页查询成功");
        return lay;
    }



    /**
     * 根据分类id查询农产品
     * @param categoryId
     * @return
     */
    @Override
    public LayuiDataGridResult getListByCategoryId(int page,int rows,String categoryId) {
        PageHelper.startPage(page,rows);
        AgriecomProduceExample example=new AgriecomProduceExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<AgriecomProduce> proList=dao.selectByExampleWithBLOBs(example);

        PageInfo<AgriecomProduce> pageInfo = new PageInfo<>(proList);

        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(proList,pageInfo.getTotal());
        lay.setCode(0);
        lay.setMsg("分页查询成功");
        return lay;
    }
    @Override
    public LayuiDataGridResult getListByCategory(String categoryId){
        AgriecomProduceExample example=new AgriecomProduceExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<AgriecomProduce> list=dao.selectByExample(example);
        for (AgriecomProduce product:list){
            product.setProduceImg(product.getProduceImg().split(",")[0]);
        }
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(list,list.size());
        lay.setMsg("success");
        return  lay;
    }

    /**
     * 根据id，produceName，produceBrief查询
     * @param page
     * @param limit
     * @param keyword
     * @return
     */

    @Override
    public LayuiDataGridResult getproduceByKeyword(int page, int limit, String keyword) {
        PageHelper.startPage(page, limit);
        List<AgriecomProduce> produceList;

        AgriecomProduceExample produceExample1=new AgriecomProduceExample();
        produceExample1.createCriteria().andProduceIdLike("%"+keyword+"%");
        produceList=dao.selectByExampleWithBLOBs(produceExample1);


        AgriecomProduceExample produceExample2=new AgriecomProduceExample();
        produceExample2.createCriteria().andProduceNameLike("%"+keyword+"%");
        produceList.addAll(dao.selectByExampleWithBLOBs(produceExample2));


        AgriecomProduceExample produceExample3=new AgriecomProduceExample();
        produceExample3.createCriteria().andProduceBriefLike("%"+keyword+"%");
        produceList.addAll(dao.selectByExampleWithBLOBs(produceExample3));


        return LayuiDataGridResult.success().add(produceList,produceList.size() );
    }



    @Override
    public List<AgriecomProduce> getHotList(int page,int limit){
        AgriecomProduceExample example=new AgriecomProduceExample();
        example.createCriteria().andIsHotEqualTo("1");//热门
        example.setOrderByClause("create_date desc");
        PageHelper.startPage(page,limit);
        List<AgriecomProduce> list= dao.selectByExample(example);
        for(AgriecomProduce product:list){
            product.setProduceImg(product.getProduceImg().split(",")[0]);
        }
        return list;
    }


    @Override
    public  List<ParamValueVo> getParamByCategoryId(String categoryId) {

        List<ParamValueVo> paramValueVoList=new ArrayList<>();

        AgriecomProduceParamExample agriecomProduceParamExample=new AgriecomProduceParamExample();
        agriecomProduceParamExample.createCriteria().andCategoryIdEqualTo(categoryId);
       List<AgriecomProduceParam> paramList=paramMapper.selectByExample(agriecomProduceParamExample);

        for(int i=0;i<paramList.size();i++){
            AgriecomProduceParamValueExample paramValueExample=new AgriecomProduceParamValueExample();
            paramValueExample.createCriteria().andParamIdEqualTo(paramList.get(i).getId());
            List<AgriecomProduceParamValue> paramValueist= paramValueMapper.selectByExample(paramValueExample);

            ParamValueVo paramValueVo=new ParamValueVo();

              paramValueVo.setParamId(paramList.get(i).getId());
              paramValueVo.setCategoryId(paramList.get(i).getCategoryId());
              paramValueVo.setParamName(paramList.get(i).getParamName());

              paramValueVo.setProduceParamValueList(paramValueist);

                paramValueVoList.add(paramValueVo);

        }

        return paramValueVoList;
    }

    @Override
    public LinkedHashMap<String,String> getAncestorsByCategoryId(String categoryId) {
    AgriecomProduceCategoryExample produceCategoryExample=new AgriecomProduceCategoryExample();
        produceCategoryExample.createCriteria().andIdEqualTo(categoryId);

        LinkedHashMap<String,String> map=new LinkedHashMap<>();
        AgriecomProduceCategory produceCategory;
        if(categoryId!=null) {
            produceCategory = cateDao.selectByPrimaryKey(categoryId);

            map.put(produceCategory.getName(), produceCategory.getId());
            while (!produceCategory.getParentId().equals("0")) {

                produceCategory = cateDao.selectByPrimaryKey(produceCategory.getParentId());
                map.put(produceCategory.getName(), produceCategory.getId());

            }
            map.put("所有分类", "0");
        }
        return map;
    }




}
